import java.io.*;
import java.util.Scanner;

/**
 * Creat by TanLei
 * Description:
 * User:86183
 * Date:2022-06-16
 * Time:13:51
 */
//实现文件拷贝操作
public class Test15 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件：!");
        String src = scanner.next();
        System.out.println("请输入要复制的目标文件！");
        String dest = scanner.next();
        //判定一下src是否存在
        File srcFile = new File(src);
        if (!srcFile.isFile()) {
            System.out.println("当前输入的路径有误，不是一个合法文件！");
            return;
        }
        //进行复制操作，关键要点，就是打开src,把内容一个字节一个字节的读出来，写入到dest中
        try (InputStream inputStream = new FileInputStream(src)) {
            try (OutputStream outputStream = new FileOutputStream(dest)) {
                //每次尝试读一个byte[] ,把这整个数组都写过去
                byte[] buffer = new byte[1024];
                while (true) {
                    //把读到的内容放入buffer中，返回实际读到的字节数中
                    int len = inputStream.read(buffer);
                    if (len == -1) {
                        break;
                    }
                    outputStream.write(buffer, 0, len);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
